package com.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.conf.RedisUtil;
import com.item.pojo.UserInfo;
import com.item.service.UserInfoService;
import com.item.utils.EmailUtils;
import com.item.vo.UserVo;
import io.netty.util.internal.StringUtil;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Beans;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class UserController {
    @Resource
    private UserInfoService service;

    @Resource
    private EmailUtils emailUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${spring.mail.username}")
    private String emailId;


    @RequestMapping("/login")
    public Map<String, Object> getLogin(String username, String password, String remember, HttpServletRequest request, HttpServletResponse response) {
        UserInfo login = service.getLogin(username, password);
        Map<String, Object> map = new HashMap<>();
        if (login != null) {
            if (remember != null) {
                Cookie usernameCookie = new Cookie("usernameCookie", username);
                Cookie passwordCookie = new Cookie("passwordCookie", password);
                Cookie rememberCookie = new Cookie("rememberCookie", "1");
                usernameCookie.setPath("/");
                passwordCookie.setPath("/");
                rememberCookie.setPath("/");
                usernameCookie.setMaxAge(1 * 60 * 60 * 24 * 3);
                passwordCookie.setMaxAge(1 * 60 * 60 * 24 * 3);
                rememberCookie.setMaxAge(1 * 60 * 60 * 24 * 3);
                response.addCookie(usernameCookie);
                response.addCookie(passwordCookie);
                response.addCookie(rememberCookie);
                System.out.println("path:" + usernameCookie.getPath());
            } else {
                Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        if ("usernameCookie".equals(cookie.getName()) || "passwordCookie".equals(cookie.getName()) || "rememberCookie".equals(cookie.getName())) {
                            cookie.setMaxAge(0);
                            response.addCookie(cookie);
                        }
                    }
                }
            }
            map.put("data", login);
        } else {
            map.put("data", "0");
        }
        return map;
    }

    @PostMapping("/email")
    public boolean getTest(String email) {
        return service.getEmail(email);
    }


    public void setRedisTime(String key, String value, Long timeOut) {
        redisTemplate.opsForValue().set(key, value);
        if (timeOut != null) {
            redisTemplate.expire(key, timeOut, TimeUnit.SECONDS);
        }
    }

    public String getRedisTime(String key) {
        return redisTemplate.opsForValue().get(key);
    }


    @PostMapping("/codeEmail")
    public boolean getCodeEmail(String email) {
        StringBuilder code = emailUtils.getEmail(emailId, email, "注册账号验证码");
        setRedisTime(email, email, 1 * 60 * 3L);
        setRedisTime(code.toString(), code.toString(), 1 * 60 * 3L);
        return code != null ? true : false;
    }

    @PostMapping("/emailCode")
    public boolean getEmailCode(String email, String that) {
        String username = (String) redisTemplate.opsForValue().get(email);
        String code = (String) redisTemplate.opsForValue().get(that);
        if (username != null && code != null) {
            return true;
        }
        return false;
    }

    @PostMapping("/verify")
    public boolean getVerify(UserVo userVo) {
        UserInfo user = new UserInfo(userVo.getNickName(), userVo.getUsername(), userVo.getPassword2());
        if (service.saveOrUpdate(user)) {
            redisTemplate.delete(userVo.getUsername());
            redisTemplate.delete(userVo.getCode());
            return true;
        }
        return false;
    }

    @PostMapping("/lookEmail")
    public boolean getUpdateCodeEmail(String email) {
        return service.getEmail(email);
    }

    @PostMapping("/updateCodeEmail")
    public boolean updateCodeEmail(String username) {
        StringBuilder emailCode = emailUtils.getEmail(emailId, username, "找回密码验证码");
        setRedisTime(username, username, 1 * 60 * 3L);
        setRedisTime(emailCode.toString(), emailCode.toString(), 1 * 60 * 3L);
        return emailCode != null ? true : false;
    }

    @PostMapping("/updatePassword")
    public boolean getUpdatePassword(String email, String code, String password2) {
        if (service.getUpdateUserPassword(email, password2)) {
            redisTemplate.delete(email);
            redisTemplate.delete(code);
            return true;
        }
        return false;
    }

    @PostMapping("/retrieve")
    public boolean getRetrievePassword(String email, String that) {
        String username = (String) redisTemplate.opsForValue().get(email);
        String code = (String) redisTemplate.opsForValue().get(that);
        if (username != null && code != null) {
            return true;
        }
        return false;
    }


}
